package com.zhiwei.cppapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.zhiwei.cppapp.R;
import com.zhiwei.cppapp.SerialPortActivity;

public class MainActivity extends SerialPortActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private MaterialDialog aboutDialog, portDialog, baudrateDialog;//提示框
    private Button btnPort, btnBaudrate, btnSend;//设置串口，波特率，发送，按钮
    private ToggleButton tbtnDisplay, tbtnSend, tbtnAuto, tbtnIO;//toggle 开关
    private TextView tvReceive;//显示接收的数据

    private String baudrateName, portName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //关联控件
        initView();
        //注册监听
        registerListener();

        MaterialDialog.Builder builder = new MaterialDialog.Builder(this);
        //关于 dialog
        builder.title("关于").content(R.string.about_msg)/*.negativeText("下一步").neutralText("取消")*/.positiveText("确定");
        aboutDialog = builder.build();
        //波特率设置dialog
        builder = new MaterialDialog.Builder(this);
        builder.title("设置波特率").items(R.array.baudrates).itemsColor(Color.BLACK).itemsGravity(GravityEnum.CENTER).itemsCallback(new MaterialDialog.ListCallback() {
            @Override
            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
                baudrateName = (String) text;
            }
        });
        baudrateDialog = builder.build();
        //设置串口设备的dialog
        builder = new MaterialDialog.Builder(this);
        builder.title("选择串口设备").items(mApplication.getDevices()).itemsColor(Color.BLACK).itemsGravity(GravityEnum.CENTER).itemsCallback(new MaterialDialog.ListCallback() {
            @Override
            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
                portName = (String) text;
            }
        });
        portDialog = builder.build();
        //设置外部点击不会取消
        portDialog.setCanceledOnTouchOutside(false);
        baudrateDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onDataReceived(byte[] buffer, int size) {

    }

    /**
     * 注册监听
     */
    private void registerListener() {
        tvReceive = (TextView) findViewById(R.id.tv_receive);
        btnPort.setOnClickListener(this);
        btnBaudrate.setOnClickListener(this);
        btnSend.setOnClickListener(this);

        tbtnAuto.setOnCheckedChangeListener(this);
        tbtnSend.setOnCheckedChangeListener(this);
        tbtnDisplay.setOnCheckedChangeListener(this);
        tbtnIO.setOnCheckedChangeListener(this);
    }

    /**
     * 关联控件
     */
    private void initView() {
        btnPort = (Button) findViewById(R.id.btn_select_port);
        btnBaudrate = (Button) findViewById(R.id.btn_select_baudrate);
        btnSend = (Button) findViewById(R.id.btn_send);
        tbtnAuto = (ToggleButton) findViewById(R.id.tbtn_send_auto);
        tbtnDisplay = (ToggleButton) findViewById(R.id.tbtn_display);
        tbtnIO = (ToggleButton) findViewById(R.id.tbtn_open);
        tbtnSend = (ToggleButton) findViewById(R.id.tbtn_send);
    }

    /**
     * 菜单
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * ActionBar上的条目菜单
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tv_about_menu:
                aboutDialog.show();
                break;
            case R.id.tv_clean_menu:
                tvReceive.setText(R.string.receive_text);
                break;
            case R.id.tv_exit_menu:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 按钮的点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_select_baudrate://选择波特率
                baudrateDialog.show();
                break;
            case R.id.btn_select_port://选择串口设备
                portDialog.show();
                break;
            case R.id.btn_send://发送数据

                break;
            default:
                break;
        }
    }

    /**
     * @param buttonView
     * @param isChecked
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
